#include "../include/kasm.h"
#include "../include/defs.h"
#include "../include/int.h"
#include "../include/syscalls.h"
#include "../include/stdIO.h"
#include "../include/gdt.h"
#include "../include/strings.h"
#include "../include/driverVideo.h"
#include "../include/getnum.h"
#include "../include/shell.h"
  
DESCR_INT idt[MAXDESCINT];			/* IDT de 255 entradas*/
IDTR idtr;				/* IDTR */

DESCR_SEG gdt[MAXDESCGDT];			/* GDT de 255 entradas*/
GDTR gdtr;				/* GDTR */

buf bufTec;				/*buffer de teclado con los Scan Codes tamaño 20*/
char bufTeclado[BUFTEC];

buf bufTecAscii;			/*buffer de teclado con los Scan Codes tamaño 40*/
char bufTecladoAscii[BUFTECASCII]; 

buf bufVid;				/*buffer de video*/
char bufVideo[BUFVID];
char *video;

int tickpos=0;

buf * fileDesc[MAXFILES];
char buff[BUFSIZE];	//buffer para ungetch
int bufp = 0;		//siguiente posicion libre en buff
/**********************************************
kmain() 
Punto de entrada de codigo C.
*************************************************/

kmain() 
{
	
        int i,num;
	
	_enable_mouse();

/* Carga de IDTR    */

	idtr.base = 0;  
	idtr.base +=(dword) &idt;
	idtr.limit = sizeof(idt)-1;
	
	_lidt (&idtr);
	

/*Cargo el File Descriptor*/
	fileDesc[0] = &bufTecAscii;
	fileDesc[1] = &bufVid; 





/* CARGA DE IDT CON LAS RUTINAS DE ATENCION DE IRQ    */
	
	setup_IDT_entry (&idt[0], 0x08, (dword)&int_00, ACS_EXC, 0);
	setup_IDT_entry (&idt[1], 0x08, (dword)&int_01, ACS_EXC, 0);
        setup_IDT_entry (&idt[2], 0x08, (dword)&int_02, ACS_EXC, 0);
	setup_IDT_entry (&idt[3], 0x08, (dword)&int_03, ACS_EXC, 0);
        setup_IDT_entry (&idt[4], 0x08, (dword)&int_04, ACS_EXC, 0);
	setup_IDT_entry (&idt[5], 0x08, (dword)&int_05, ACS_EXC, 0);
        setup_IDT_entry (&idt[6], 0x08, (dword)&int_06, ACS_EXC, 0);
	setup_IDT_entry (&idt[7], 0x08, (dword)&int_07, ACS_EXC, 0);
        setup_IDT_entry (&idt[8], 0x08, (dword)&int_08, ACS_EXC, 0);
	setup_IDT_entry (&idt[9], 0x08, (dword)&int_09, ACS_EXC, 0);
	setup_IDT_entry (&idt[10], 0x08, (dword)&int_0A, ACS_EXC, 0);
        setup_IDT_entry (&idt[11], 0x08, (dword)&int_0B, ACS_EXC, 0);
        setup_IDT_entry (&idt[12], 0x08, (dword)&int_0C, ACS_EXC, 0);
        setup_IDT_entry (&idt[13], 0x08, (dword)&int_0D, ACS_EXC, 0);
        setup_IDT_entry (&idt[14], 0x08, (dword)&int_0E, ACS_EXC, 0);

	setup_IDT_entry (&idt[16], 0x08, (dword)&int_10, ACS_EXC, 0);
        setup_IDT_entry (&idt[17], 0x08, (dword)&int_11, ACS_EXC, 0);
	setup_IDT_entry (&idt[18], 0x08, (dword)&int_12, ACS_EXC, 0);
        setup_IDT_entry (&idt[19], 0x08, (dword)&int_13, ACS_EXC, 0);
	setup_IDT_entry (&idt[0x20], 0x08, (dword)&_int_20_hand, ACS_INT, 0);
	setup_IDT_entry (&idt[0x21], 0x08, (dword)&_int_21_hand, ACS_INT, 0);
	setup_IDT_entry (&idt[0x80], 0x08, (dword)&_int_80_hand, ACS_INT, 0);

	/* CARGA DE IDT CON LA RUTINA DE ATENCION DE LA IRQ12   */
// irq12 o sea la 5ta irq del pic 2
 				
	setup_IDT_entry (&idt[0x2c], 0x08, (dword)&_int_32_hand, ACS_INT, 0);



/* Habilito interrupcion de timer tick*/
/*Muevo el vector de interrupciones del pic*/
	
	_Cli();

	_set_pic_int(); 

        _mascaraPIC1(0xF8);
        _mascaraPIC2(0xEF);//F7 ef

	_Sti();

	inicializarVideo();
	inicializarTeclado();
	inicializarGdt();

	print("Presione una tecla para continuar \n\0");

	getc(0);
	resetVideo();
	k_clear_screen();

	my_shell shell_1;
	my_shell shell_2;

	buf comando_1;
	buf comando_2;


	char buffComando_1[160];
	char buffComando_2[160];
	
	comando_1.buf = buffComando_1;
	comando_1.offset = 0;
	comando_1.siguiente = 0;
	comando_1.size = 160;

	comando_2.buf = buffComando_2;
	comando_2.offset = 0;
	comando_2.siguiente = 0;
	comando_2.size = 160;

	char buffShell_1[80*25];
	char buffShell_2[80*25];
	
	shell_1.buf = buffShell_1;
	shell_1.comando_actual = comando_1;
	shell_1.offset = 0;
	shell_1.siguiente = 0;
	shell_1.size = 80*25;	

	shell_2.buf = buffShell_2;
	shell_2.comando_actual = comando_2;	
	shell_2.offset = 0;
	shell_2.siguiente = 0;
	shell_2.size = 80*25;	

  char name [] = {'<','S','m','i','l','e',' ','3','.','0','>',':','\0'};	

	 int j;
	 for(j=0; j<12; j++){
 	 	shell_1.buf[shell_1.siguiente++] = name[j];
		shell_2.buf[shell_2.siguiente++] = name[j];
      		putc(name[j],1);
         }	


	int shell_num=1;

	do{
		if (shell_num==1)		
			shell_num = shell(&shell_1,shell_num);
		else if(shell_num == 2)
			shell_num = shell(&shell_2,shell_num);
	}while(1);
	
	while(1)
        {
        }
	
}

